﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DiscoveryLogic.UI.Layouts.WS;

namespace DiscoveryLogic.UI.Layouts
{
    public partial class App : Application
    {


        public App()
        {
            //Can customise the culture here, like so
            //System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-FR");

            InitializeComponent();

            this.Startup += this.OnStartup;
            this.Exit += this.OnExit;
            this.UnhandledException += new EventHandler<ApplicationUnhandledExceptionEventArgs>(App_UnhandledException);


        }

        bool _useWebServices = false;

        public bool UseWebServices
        {
            get
            {
                return _useWebServices;
            }
        }


        void onStatementsCompleted(GetStatementsCompletedEventArgs args)
        {
        }


        bool _accountsCompleted = false;
        bool _payeesCompleted = false;
        bool _mortgageCompleted = true;
        bool _portfolioCompleted = false;

        private void OnStartup(object sender, StartupEventArgs e)
        {

            //LocalData.DataFileGenerator.CreateStatementData();
            //LocalData.DataFileGenerator.CreateFundsData(); 

            if (UseWebServices)
            {
                try
                {
                    AccountService svc = new AccountService();
                    svc.GetAccountsAsync(new AccountService.GetAccountsCallBack(onAccountsCompleted));

                    StatementService ss = new StatementService();
                    ss.GetStatementAsync(new StatementService.GetStatementsCompletedCallBack(onStatementsCompleted));


                    BillPayService bps = new BillPayService();
                    bps.GetPayeesAsync(new BillPayService.GetPayeesCallBack(onPayeesCompleted));


                    MortgageService ms = new MortgageService();
                    ms.GetMortgageAsync(new MortgageService.GetMortgageCompletedCallBack(onMortgageCompleted));
                }
                catch
                {

                }
            }
            else
                this.RootVisual = new DashboardForm();

            // Load the main control
            // this.RootVisual = new Page();
            //    UnhandledException += new EventHandler<ApplicationUnhandledExceptionEventArgs>(App_UnhandledException);

        }



        void ShowMain()
        {

            if (this._accountsCompleted) // && this._mortgageCompleted && this._payeesCompleted && _portfolioCompleted )
            {
                this.RootVisual = new DashboardForm();
            }
        }


        void onMortgageCompleted(GetMortgageCompletedEventArgs args)
        {
            System.Diagnostics.Debug.WriteLine("Mortgage Web Service Completed");
            this._mortgageCompleted = true;
            this.ShowMain();
        }


        void onAccountsCompleted(GetAccountsCompletedEventArgs args)
        {
            System.Diagnostics.Debug.WriteLine("Accounts Web Service Completed");
            this._accountsCompleted = true;
            this.ShowMain();
        }

        void onPayeesCompleted(GetPayeesCompletedEventArgs args)
        {
            System.Diagnostics.Debug.WriteLine("Payees Completed");
            this._payeesCompleted = true;
            this.ShowMain();
        }

        void onPortfolioCompleted(GetPortfolioCompletedEventArgs args)
        {
            System.Diagnostics.Debug.WriteLine("Portfolio Completed");
            this._portfolioCompleted = true;
            this.ShowMain();
        }


        void App_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {

            System.Diagnostics.Debug.WriteLine(e.ExceptionObject.Message);
            System.Diagnostics.Debug.WriteLine(e.ExceptionObject.StackTrace);

        }

        private void OnExit(object sender, EventArgs e)
        {

        }
    }
}
